package ink.zhaohui.book.servicecomb.impl;

import java.util.Date;
import java.util.UUID;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import ink.zhaohui.book.service.IBookService;
import ink.zhaohui.book.servicecomb.BookEndpoint;
import ink.zhaohui.book.servicecomb.InstanceInfoService;
import ink.zhaohui.book.tool.Helper;
import ink.zhaohui.book.vo.BookVO;
import ink.zhaohui.book.vo.Page;
import ink.zhaohui.book.vo.UpdateVO;

@RestSchema(schemaId = "BookRestEndpoint")
@RequestMapping(path = "/book")
public class BookRestEndpoint implements BookEndpoint {

	private final IBookService bookService;

	private final InstanceInfoService instanceInfoService;

	@Autowired
	public BookRestEndpoint(InstanceInfoService instanceInfoService, IBookService bookService) {
		this.bookService = bookService;
		this.instanceInfoService = instanceInfoService;
	}
	
	@GetMapping(path = "/queryOneByUuid")
	@Override
	public BookVO queryOneByUuid(String uuid) {
		// TODO Auto-generated method stub
		BookVO v=bookService.queryOneByUuid(uuid);
		v.setInstanceId(instanceInfoService.getInstanceId());
		v.setCallTime(Helper.date2string(new Date()));
		return v;
	}
	
	@GetMapping(path = "/queryOneByOrderUuid")
	@Override
	public BookVO queryOneByOrderUuid(String orderUuid) {
		// TODO Auto-generated method stub
		BookVO v=bookService.queryOneByOrderUuid(orderUuid);
		v.setInstanceId(instanceInfoService.getInstanceId());
		v.setCallTime(Helper.date2string(new Date()));
		return v;
	}
	
	@GetMapping(path = "/queryMore")
	@Override
	public Page<BookVO> queryMore(String name, int pageSize, int at) {
		// TODO Auto-generated method stub
		Page<BookVO> p=bookService.queryMore(name, pageSize, at);
		p.setInstanceId(instanceInfoService.getInstanceId());
		p.setCallTime(Helper.date2string(new Date()));
		return p;
	}
	
	@GetMapping(path = "/bookOne")
	@Override
	public BookVO bookOne(BookVO bookVO) {
		// TODO Auto-generated method stub
		bookVO.setInstanceId(instanceInfoService.getInstanceId());
		bookVO.setCallTime(Helper.date2string(new Date()));
		return bookService.bookOne(bookVO);
	}
	
	@GetMapping(path = "/cancelBook")
	@Override
	public UpdateVO cancelBook(String uuid) {
		// TODO Auto-generated method stub
		int n=bookService.cancelBook(uuid);
		UpdateVO u=new UpdateVO();
		u.setCount(n);
		u.setInstanceId(instanceInfoService.getInstanceId());
		u.setCallTime(Helper.date2string(new Date()));
		return u;
	}
	
	@GetMapping(path = "/finishBook")
	@Override
	public UpdateVO finishBook(String uuid) {
		// TODO Auto-generated method stub
		int n=bookService.finishBook(uuid);
		UpdateVO u=new UpdateVO();
		u.setCount(n);
		u.setInstanceId(instanceInfoService.getInstanceId());
		u.setCallTime(Helper.date2string(new Date()));
		return u;
	}
	
	@GetMapping(path = "/cancelFinishBook")
	@Override
	public UpdateVO cancelFinishBook(String uuid) {
		// TODO Auto-generated method stub
		int n=bookService.cancelFinishBook(uuid);
		UpdateVO u=new UpdateVO();
		u.setCount(n);
		u.setInstanceId(instanceInfoService.getInstanceId());
		u.setCallTime(Helper.date2string(new Date()));
		return u;
	}

}
